#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include "cgic.h"

int fd;

int filter_func(const struct dirent *entry)
{
    // 这里可以根据需求编写过滤条件

    if (strstr(entry->d_name, ".mp4") != NULL)
    {
        return 1;
    }

    return 0;
}

int cgiMain()
{
    struct dirent *entry;
    struct dirent **entryList;
    int size;
    char name[32] = {0};

    size = scandir("/demo/bin/boa/www/Video", &entryList, filter_func, alphasort);
    //size = scandir("/userdata/", &entryList, filter_func, alphasort);
    cgiHeaderContentType("text/html");
    fprintf(cgiOut, "<HTML>\n");
    fprintf(cgiOut, "<TITLE>Picture Manage</TITLE>\n");
    fprintf(cgiOut, "<HEAD>\n");
    fprintf(cgiOut, "<link rel='stylesheet' href='../common.css'>\n");
    fprintf(cgiOut, "</HEAD>\n");
    fprintf(cgiOut, "<BODY>\n");
    fprintf(cgiOut, "<ul class='menu'>\n");
    fprintf(cgiOut, "<li><a href='/cgi-bin/scan_dir_cgi.cgi'>Photo_Manage</a></li>\n");
    fprintf(cgiOut, "<li><a href='../ffmpeg_config.html'>Edit_URL</a></li>\n");
    fprintf(cgiOut, "<li><a href='/cgi-bin/scan_dir_cgi.cgi'>URL_Manage</a></li>\n");
    fprintf(cgiOut, "<li><a href='../decode_video.html'>Remote_Moniting</a></li>\n");
    fprintf(cgiOut, "<li><a href='/cgi-bin/scan_video_cgi.cgi'>Video_Manage</a></li>\n");
    fprintf(cgiOut, "</ul>\n");

    fprintf(cgiOut, "<ul class='video-list'>\n");
#if 1
    for (int i = 0; i < size; i++)
    {
        entry = entryList[i];
        memset(name, 0, sizeof(name));
        strcpy(name, entry->d_name);
        fprintf(cgiOut, "<li>\n");
        fprintf(cgiOut, "<video width=640 height=480 controls>\n");
        fprintf(cgiOut, "<source src='/Video/%s' type=video/mp4>\n", name);
        fprintf(cgiOut, "</video>\n");
        fprintf(cgiOut, "<H2>%s</H2>\n", name);
        fprintf(cgiOut, "</li>\n");
    }
#endif
    fprintf(cgiOut, "</ul>\n");

    fprintf(cgiOut, "</BODY>\n");
    fprintf(cgiOut, "</HTML>\n");
    return 0;
}
